package com.fulihui.wgj.consumer.communication.event;

import com.fulihui.common.constants.TableNameConstants;
import com.fulihui.common.enums.EventTypeEnum;
import com.fulihui.common.enums.HasClickLinkEnum;
import com.fulihui.common.enums.MarkSceneEnum;
import com.fulihui.common.util.TenMinutesMsgUtil;
import com.fulihui.open.shortlink.model.ShortUrl;
import com.fulihui.stater.kudu.client.KuduImpalaTemplate;
import com.fulihui.wgj.consumer.entity.WxUserMarkInfo;
import com.fulihui.wgj.consumer.service.ShortLinkService;
import com.fulihui.wgj.facade.dto.WxUserInfoDTO;
import com.fulihui.wgj.facade.msg.EventMsg;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kudu.client.Insert;
import org.apache.kudu.client.KuduException;
import org.apache.kudu.client.Operation;
import org.apache.kudu.client.Upsert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * @author: LiuJie
 * @version: 2018/7/7 14:08
 * @description: 菜单展示
 */
@Component
@Slf4j
public class ViewEventProcessor implements EventMsgProcessor {
    @Autowired
    private TenMinutesMsgUtil tenMinutesMsgUtil;
    @Autowired
    private ShortLinkService shortLinkService;
    @Autowired
    private KuduImpalaTemplate kuduImpalaTemplate;
    @Override
    public void process(EventMsg message) {
        String openid = message.getFromUserName();
        String appid = message.getAppId();
        String viewUrl = message.getEventKey();
        if (StringUtils.isBlank(appid)
                || StringUtils.isBlank(openid)) {
            log.error("VIEW 事件 appid 或 openid 为空,eventMsg={}",message);
            return;
        }

        //查询短链 Url 状态
        ShortUrl urlDetail = shortLinkService.queryUrl(openid, appid, viewUrl);
        // 如果不为空且是监控状态
        if (urlDetail != null && urlDetail.getMonitor() == 1) {
            // 十分钟拉活消息 - 点击过菜单了删除有操作用户
            tenMinutesMsgUtil.rmSubscribe(appid,openid);
        }
        // 标记用户为已点击
        WxUserInfoDTO user = new WxUserInfoDTO();
        user.setAppId(appid);
        user.setOpenid(openid);
        user.setHasClickLink(HasClickLinkEnum.HAS.getValue());

        //插入点击行为记录
        WxUserMarkInfo info = new WxUserMarkInfo();
        info.setAppid(appid);
        info.setOpenid(openid);
        info.setId(kuduImpalaTemplate.getId());
        info.setGmtCreate(System.currentTimeMillis());
        info.setGmtModifed(System.currentTimeMillis());
        info.setMarkScene(MarkSceneEnum.MENU_VIEW.getCode());
        info.setMarkLink(viewUrl);

        try {
            // 执行更新
            Upsert upsertUser = kuduImpalaTemplate.createUpsert(appid, user);
            Insert insertUserMarkInfo = kuduImpalaTemplate.createInsert(TableNameConstants.USER_MARK_INFO, info);
            List<Operation> list = new LinkedList<>();
            list.add(upsertUser);
            list.add(insertUserMarkInfo);
            kuduImpalaTemplate.apply(list);
        } catch (KuduException e) {
            e.printStackTrace();
        }
        log.info("User HasClick recode Success! user = {}",user);
    }



    @Override
    public String getEvent() {
        return EventTypeEnum.VIEW.getCode();
    }
}
